package org.cook.utils.html.yyyy;

import java.io.File;

import org.cook.utils.common.Constants;
import org.cook.utils.common.Utils;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class ExtractElementByXPath {

	public static void main(String[] args) throws Exception {
		HtmlCleaner cleaner = new HtmlCleaner();
		cleaner.getProperties().setCharset(Constants.ENCODING_GB2312);
		
		String target = "/storage/shared/yueyang-hospital/yymyc/www.yymyc.com/cn/zhjia/ylb-1.htm";
		
		TagNode node = cleaner.clean(new File(target));
		TagNode[] bodyNodes = node.getElementsByName("body", false);
		
		for (TagNode body : bodyNodes) {
			Object[] northDoctors = body.evaluateXPath("//td[@class='style16']");
			System.out.println(northDoctors.length);
			
			String content = "";
//			for (int i=0; i<northDoctors.length; i++) {
//				if (i%7 == 0) {
//					System.out.println();
//				}
//				content = ((TagNode) northDoctors[i]).getText().toString();
//				System.out.print(Utils.replaceAll(content.substring(0, 3), "\\s+", "") + "  |  ");
//			}
			
			Object[] allDoctors = body.evaluateXPath("//tr[@height='14']/td");
			System.out.println(allDoctors.length);
			
			int j=1;
			for (int i=0; i<allDoctors.length; i++) {
				if (j%8 == 0) {
					j++;
					System.out.println("j-" + j);
				}
				content = ((TagNode) allDoctors[i]).getText().toString().trim();
				content = Utils.replaceAll(content, "\\r+", "");
				content = Utils.replaceAll(content, "\\n+", "");
				
				if (content.length() > 5) {
					j++;
					System.out.print(Utils.replaceAll(content.substring(0, 3), "\\s+", "") + "  |  ");
				}
			}
			
			System.out.println(j);
		}
			
	}
	
}
